package edu.umn.actvs;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import edu.umn.ftpmaster.Command;
import edu.umn.ftpmaster.Location_stp;

public class MainPanelActivity extends Activity implements LocationListener{

	private Button listbtn;
	private Button searchbtn;
	private Button showbtn;
	private EventDBCreator creator;
	private LocationManager  locationManager;
	private String bestProvider;
	private double lat,longt;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.mainpanel);
		creator = new EventDBCreator(this);

		searchbtn = (Button)findViewById(R.id.searchbtn);
		listbtn = (Button)findViewById(R.id.listbtn);
		showbtn = (Button)findViewById(R.id.showbtn);
		
		
		locationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
		Criteria criteria = new Criteria();
		bestProvider = locationManager.getBestProvider(criteria, false);
		Location lastKnownLocation = locationManager.getLastKnownLocation(bestProvider);
		
//		String locationProvider = locationManager.NETWORK_PROVIDER;
		//		// Or use LocationManager.GPS_PROVIDER
		if(lastKnownLocation != null){
				lat = lastKnownLocation.getLatitude();
				longt=lastKnownLocation.getLongitude();
		}
		else{
			lat = 0;
			longt =0;
		}
		
    	Log.w("out_lat", Double.toString(lat));
    	Log.w("out_longt", Double.toString(longt));
		

		searchbtn.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View view) {
				Intent intent = new Intent(MainPanelActivity.this, SearchActivity.class);
				startActivity(intent);
			}
		});
		listbtn.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View view) {
				
				Location_stp loc = new Location_stp();
				loc.latitude=lat;
				loc.longitude=longt;
				loc.time = System.currentTimeMillis();

				Command cmd = new Command("search_loc",loc,null,null,null,null);
				Command resp = null;
				try {
					 resp = CommandHelper.sendCommand(Constants.SERVERIP, Constants.SERVERPORT, cmd);
				} catch (ClassNotFoundException e) {

					e.printStackTrace();
				}

				String test = "event01|user01|location01|:event02|user02|location02|";
				String res = resp.getMessage();
				//Log.w("create resp", res);
				Intent intent = new Intent(MainPanelActivity.this, ShowEventActivity.class);
				intent.putExtra("event", res);
				startActivityForResult(intent,1);
			}
		});

		showbtn.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View view) {
				Intent intent = new Intent(MainPanelActivity.this, UserEventActivity.class);
				startActivity(intent);
			}
		});
	}
	
	
	protected void onActivityResult(int requestCode, int resultCode, Intent data)
	{
		if(requestCode ==1 && resultCode ==RESULT_OK){//list events around me
			String selectedEvents = data.getStringExtra("selectedEvents");
			
	//		Log.w("selected events", selectedEvents);
		}
		else if(requestCode ==2 && resultCode== RESULT_OK){//search events
			
		}
		else if(requestCode ==3 && resultCode== RESULT_OK){//my events
		
		}
	}
	
	protected void onResume(){
		super.onResume();
		locationManager.requestLocationUpdates(bestProvider, 20000, 2,this);
	}
	protected void onPause(){
		super.onPause();
		locationManager.removeUpdates(this);
	}

	@Override
	public void onLocationChanged(Location location) {
		
		if(Math.abs(location.getLatitude())>1  && Math.abs(location.getLongitude())>1)
		{
			lat = location.getLatitude();
			longt=location.getLongitude();
    	
			Log.w("update_lat", Double.toString(lat));
			Log.w("update_longt", Double.toString(longt));
	
		}
	}


	@Override
	public void onProviderDisabled(String arg0) {
		// TODO Auto-generated method stub
		
	}


	@Override
	public void onProviderEnabled(String arg0) {
		// TODO Auto-generated method stub
		
	}


	@Override
	public void onStatusChanged(String arg0, int arg1, Bundle arg2) {
		// TODO Auto-generated method stub
		
	}
}
